package com.baosight.sczx.common.zm.cInterface.cs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkSchedule;
/**
 * 调度台帐获取班次的接口
 * <p>Title: WorkSchedule.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-3-11 下午01:48:16 余虎  Create
 *</pre>
 */
public class WorkSchedule {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 
	 * <p>Function: getWorkScheduleBlock</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午11:05:06 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static EiBlock getWorkScheduleBlock(String blockNo,String miningId, boolean isAll){                                                              
		EiBlock block = new EiBlock(blockNo);
		
		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("chineseValue");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map>  _list = new ArrayList<Map>();
		if(isAll) {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("value", "");
			_map.put("chineseValue", "");
			_map.put("display", "");
			_list.add(_map);
		}

		List<Map> temp = getWorkSchedule(miningId);
		for(int i = 0; i < temp.size(); i++) {
			_list.add(temp.get(i));
		}
		block.setRows(_list);
		
		return block;
	}
	
	/**
	 * 
	 * <p>Function: getWorkSchedule</p>
	 * <p>Description: </p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午11:05:02 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public static List getWorkSchedule(String miningId)
	{		
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("fdMiningId", miningId);
		return dao.query("ZMCSWorkSchedule.queryAll", map);
	}
	
	/**
	 * 获得当前时间下的 默认班次
	 * <p>Function: getDefaultWorknum</p>
	 * <p>Description: 获得默认班次</p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午11:05:02 高强
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static EiBlock getDefaultWorknum()
	{
		EiBlock workNumblock = new EiBlock("defaultWorknumBlock");
		workNumblock.addBlockMeta(initMetaData());
		String curTime = StaticTools.getTime();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdStartTime", curTime);
		_map.put("fdEndTime", curTime);
		List<Map>  result = new ArrayList();
		result = dao.query("ZMCSWorkSchedule.query", _map);
		
		if(result!=null && result.size() > 0) {
			workNumblock.addRows(result);
		}
		return workNumblock;
	}
	
	/**
	 * 根据当前的日期和班次设置，获取调度日期
	 * <p>Function: getVirtualDate</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8下午03:01:37 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked" })
	public static String getVirtualDate(String inputDate, String worknumId)
	{
		List<ZMCSWorkSchedule>  result = dao.query("ZMCSWorkSchedule.query", null);
		
		if(result.size()>0)
		{	
			String type="", start="";
			for(int i=0;i<result.size();i++)
			{
				if(result.get(i).getFdKeyId().equals(worknumId))
				{
					type = result.get(i).getFdScheduleClassify();
					start = inputDate + " " + result.get(i).getFdStartTime();
//					if(result.get(i).getFdIsThrough()==1)
//					{
//						start = StaticTools.getDate(1, inputDate, "yyyy-MM-dd") + " " + result.get(i).getFdStartTime();
//					}						
				}
			}
			for(int i=0;i<result.size();i++)
			{				
				if(result.get(i).getFdIsStart()==1 && type.equals(result.get(i).getFdScheduleClassify()) && "12:00".compareTo(result.get(i).getFdStartTime())<0)
				{
					String dateStr=inputDate + " " + result.get(i).getFdStartTime();
//					if(result.get(i).getFdIsThrough()==1)
//					{
//						dateStr = StaticTools.getDate(1, inputDate, "yyyy-MM-dd") + " " + result.get(i).getFdStartTime();
//					}
					
					if(dateStr.compareTo(start)<=0)
					{
						return StaticTools.getDate(1, inputDate, "yyyy-MM-dd");
					}
				}
			}
		}
		return inputDate;
	}
	
	/**
	 * 根据班次获得班次起始时间
	 * <p>Function: getVirtualDate</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-4-18下午03:01:37 高强
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked" })
	public static ZMCSWorkSchedule getWrokStartTime(String worknumId)
	{
		List<ZMCSWorkSchedule>  result = dao.query("ZMCSWorkSchedule.query", null);
		if(result.size()>0)
		{	
			
			for(int i=0;i<result.size();i++)
			{
				if(result.get(i).getFdKeyId().equals(worknumId))
				{
					return result.get(i);
				}
			}
		}
		return null;
	}
	
	/**
	 * 构造表头信息
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlockMeta</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-25上午07:32:02 严洪浩
	 * </p>
	 *
	 */
	public static EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();
        EiColumn eiColumn;
		
		eiColumn = new EiColumn("fdKeyId");
		eiColumn.setPrimaryKey(true);
		eiColumn.setFieldLength(32);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdScheduleNum");
		eiColumn.setFieldLength(50);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdScheduleClassify");
		eiColumn.setFieldLength(50);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdIsStart");
		eiColumn.setType("C");
		eiColumn.setScaleLength(0);
		eiColumn.setFieldLength(38);
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdIsThrough");
		eiColumn.setType("N");
		eiColumn.setScaleLength(0);
		eiColumn.setFieldLength(38);
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdStartTime");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdEndTime");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdRemark");
		eiColumn.setFieldLength(500);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("starthour");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("startminute");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("endhour");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("endminute");
		eiColumn.setFieldLength(14);	
		eiColumn.setDescName(" ");
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}
}
